#include "Queue.h"

void InitQueue(queue* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
	pq->size = 0;
}

void PushQueue(queue* pq, QDatatype x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc false");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;
		pq->size++;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = pq->tail->next;
		pq->size++;
	}
}

void PopQueue(queue* pq)
{
	assert(pq);
	assert(pq->head);
	QNode* newhead = pq->head->next;
	free(pq->head);
	pq->head = newhead;
	pq->size--;
}

void DestoryQueue(queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(pq);
	pq = NULL;
}

int SizeQueue(queue* pq)
{
	assert(pq);
	return pq->size;
}

int QueueEmpty(queue* pq)
{
	assert(pq);
	if (pq->size)
	{
		return pq->size;
	}
	else
	{
		return 0;
	}
}

QDatatype QueueFront(queue* pq)
{
	assert(pq);
	return pq->head->data;
}

QDatatype TailQueue(queue* pq)
{
	assert(pq);
	return pq->tail->data;
}

void Print(queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
